//
//  YLChangePasswordViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/11/10.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLChangePasswordViewModel.h"
#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "NSString+Hash.h"
#import "WHProgressHUD.h"
#import "NSString+Regex.h"
@implementation YLChangePasswordViewModel


- (instancetype)init
{
    if (self = [super init]) {
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    _passwordChangeEnableSiganl = [RACSignal combineLatest:@[RACObserve(self, passwordNew), RACObserve(self, passwordOriginal), RACObserve(self, passwordSure)] reduce:^id _Nullable(NSString *passwordNew, NSString *passwordOriginal, NSString *passwordSure){
        return @(passwordNew.length && passwordSure.length && passwordOriginal.length);
    }];
}

- (RACCommand *)requestCommand
{
    if (!_requestCommand) {
        __weak typeof(self) weakSelf = self;
        _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {

            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                // 判断密码
                {
                    NSString *message = [NSString isOrNoPasswordStyle:weakSelf.passwordNew];
                    if (message.length != 0) {
                        [WHProgressHUD showError:message];
                        [subscriber sendCompleted];
                        return nil;
                    }
                }
                
                
                if (![weakSelf.passwordSure isEqualToString:weakSelf.passwordSure]) {
                    [WHProgressHUD showError:@"2次密码不一样"];
                    [subscriber sendCompleted];
                    return nil;
                }
                
                
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/ModifyPassWord", YLURL];
                [WHProgressHUD showStatus:@"正在修改密码，请勿退出..."];
                NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:3];
                params[@"OldPassWord"] = [weakSelf.passwordOriginal md5String];
                params[@"NewPassWord"] = [weakSelf.passwordNew md5String];
                params[@"NewPassWordAgain"] = [weakSelf.passwordSure md5String];
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:params success:^(id responseObject) {
                    [WHProgressHUD dismiss];
                    
                    
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        [WHProgressHUD showSuccess:@"修改密码成功"];
                        [subscriber sendNext:nil];
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    
                    [subscriber sendCompleted];
                } fail:^(NSError *error) {
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _requestCommand;
}

- (void)dealloc
{
    
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
}


@end
